package com.ylsk.system.service.impl;

import java.util.List;

import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.DateUtils;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.common.utils.ip.IpUtils;
import com.ylsk.system.domain.FileInfo;
import com.ylsk.system.domain.FileSimple;
import com.ylsk.system.helper.FileHelper;
import com.ylsk.system.service.IMinioFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ylsk.system.mapper.SysApkPackageMapper;
import com.ylsk.system.domain.SysApkPackage;
import com.ylsk.system.service.ISysApkPackageService;

/**
 * apk包管理Service业务层处理
 *
 * @author ysy
 * @date 2024-01-24
 */
@Service
public class SysApkPackageServiceImpl implements ISysApkPackageService
{
    @Autowired
    private SysApkPackageMapper sysApkPackageMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private FileHelper fileHelper;

    @Value("${server.port}")
    private String port ;

    /**
     * 查询apk包管理
     *
     * @param apkPackageId apk包管理主键
     * @return apk包管理
     */
    @Override
    public SysApkPackage selectSysApkPackageByApkPackageId(Long apkPackageId)
    {
        SysApkPackage sysApkPackage = sysApkPackageMapper.selectSysApkPackageByApkPackageId(apkPackageId);
        Long apkPackageFileId = sysApkPackage.getApkPackageFileId();
        List<FileSimple> apkPackageFileData = fileHelper.getFileByBusinessId(apkPackageFileId);
        sysApkPackage.setApkPackageFileData(apkPackageFileData);
        return sysApkPackage;
    }

    /**
     * 查询apk包管理列表
     *
     * @param sysApkPackage apk包管理
     * @return apk包管理
     */
    @Override
    public List<SysApkPackage> selectSysApkPackageList(SysApkPackage sysApkPackage)
    {
        return sysApkPackageMapper.selectSysApkPackageList(sysApkPackage);
    }

    /**
     * 新增apk包管理
     *
     * @param sysApkPackage apk包管理
     * @return 结果
     */
    @Override
    public int insertSysApkPackage(SysApkPackage sysApkPackage)
    {
        long apkPackageFileId = snowIdConfig.getSnowId();
        String apkPackageFile = sysApkPackage.getApkPackageFile();
        if(StringUtils.isNotEmpty(apkPackageFile)){
            fileHelper.saveBusinessFileData(apkPackageFile,apkPackageFileId);
            FileInfo fileInfo = fileHelper.getFileInfo(Long.valueOf(apkPackageFile.split(",")[0]));
            sysApkPackage.setQrCodeUrl(IpUtils.getHostIp() + ":" + port + "/system/file/apk-download");
            sysApkPackage.setPackageSize(fileInfo.getFileSize().toString());
        }

        sysApkPackage.setApkPackageFileId(apkPackageFileId);
        sysApkPackage.setApkPackageId(snowIdConfig.getSnowId());
        return sysApkPackageMapper.insert(sysApkPackage);
    }

    /**
     * 修改apk包管理
     *
     * @param sysApkPackage apk包管理
     * @return 结果
     */
    @Override
    public int updateSysApkPackage(SysApkPackage sysApkPackage)
    {
        Long apkPackageId = sysApkPackage.getApkPackageId();
        if(null == apkPackageId){
            throw new ServiceException("参数异常!");
        }
        SysApkPackage _sysApkPackage = sysApkPackageMapper.selectById(apkPackageId);

        String apkPackageFile = sysApkPackage.getApkPackageFile();
        if(StringUtils.isNotEmpty(apkPackageFile)){
            FileInfo fileInfo = fileHelper.getFileInfo(Long.valueOf(apkPackageFile.split(",")[0]));
            sysApkPackage.setQrCodeUrl(IpUtils.getHostIp() + ":" + port + "/system/file/apk-download");
            sysApkPackage.setPackageSize(fileInfo.getFileSize().toString());
        }
        fileHelper.updateBusinessFileData(apkPackageFile,_sysApkPackage.getApkPackageFileId());
        return sysApkPackageMapper.updateById(sysApkPackage);
    }

    /**
     * 批量删除apk包管理
     *
     * @param apkPackageIds 需要删除的apk包管理主键
     * @return 结果
     */
    @Override
    public int deleteSysApkPackageByApkPackageIds(Long[] apkPackageIds)
    {
        return sysApkPackageMapper.deleteSysApkPackageByApkPackageIds(apkPackageIds);
    }

    /**
     * 删除apk包管理信息
     *
     * @param apkPackageId apk包管理主键
     * @return 结果
     */
    @Override
    public int deleteSysApkPackageByApkPackageId(Long apkPackageId)
    {
        return sysApkPackageMapper.deleteSysApkPackageByApkPackageId(apkPackageId);
    }
}
